
package com.jf.cloud.im.service.impl;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.im.constants.WhoSend;
import com.jf.cloud.im.mapper.ImMsgBizEmployeeRecentMapper;
import com.jf.cloud.im.model.ImMsgBizEmployeeRecent;
import com.jf.cloud.im.model.ImMsgBizUserHistory;
import com.jf.cloud.im.service.ImMsgBizEmployeeRecentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * 员工最近聊天记录表
 *
 * @author zz
 * @date 2021-08-25 13:58:56
 */
@Service
public class ImMsgBizEmployeeRecentServiceImpl implements ImMsgBizEmployeeRecentService {

    @Autowired
    private ImMsgBizEmployeeRecentMapper imMsgBizEmployeeRecentMapper;

    @Override
    public PageVO<ImMsgBizEmployeeRecent> page(PageDTO pageDTO) {
        return PageUtil.doPage(pageDTO, () -> imMsgBizEmployeeRecentMapper.list());
    }

    @Override
    public PageVO<ImMsgBizEmployeeRecent> pageBySendTypeAndBizIdAndEmployeeId(PageDTO pageDTO, Integer sendType, Long bizId, Long employeeId) {
        return PageUtil.doPage(pageDTO, () -> imMsgBizEmployeeRecentMapper.listBySendTypeAndBizIdAndEmployeeId(sendType, bizId, employeeId));
    }

    @Override
    public ImMsgBizEmployeeRecent getById(Long id) {
        return imMsgBizEmployeeRecentMapper.getById(id);
    }

    @Override
    public ImMsgBizEmployeeRecent getBySendTypeBizIdEmployeeIdUserId(Integer sendType, Long bizId, Long employeeId, Long userId) {
        return imMsgBizEmployeeRecentMapper.getBySendTypeBizIdEmployeeIdUserId(sendType, bizId, employeeId, userId);
    }

    @Override
    public void save(ImMsgBizEmployeeRecent imMsgBizEmployeeRecent) {
        imMsgBizEmployeeRecentMapper.save(imMsgBizEmployeeRecent);
    }

    @Override
    public void update(ImMsgBizEmployeeRecent imMsgBizEmployeeRecent) {
        imMsgBizEmployeeRecentMapper.update(imMsgBizEmployeeRecent);
    }

    @Override
    public void deleteById(Long id) {
        imMsgBizEmployeeRecentMapper.deleteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateEmployeeRecent(ImMsgBizUserHistory imMsgBizUserHistory) {
        ImMsgBizEmployeeRecent imMsgBizEmployeeRecent = this.getBySendTypeBizIdEmployeeIdUserId(imMsgBizUserHistory.getSendType(), imMsgBizUserHistory.getBizId(), imMsgBizUserHistory.getEmployeeId(), imMsgBizUserHistory.getUserId());
        if (Objects.isNull(imMsgBizEmployeeRecent)) {
            imMsgBizEmployeeRecent = new ImMsgBizEmployeeRecent();
            imMsgBizEmployeeRecent.setSendType(imMsgBizUserHistory.getSendType());
            imMsgBizEmployeeRecent.setBizId(imMsgBizUserHistory.getBizId());
            imMsgBizEmployeeRecent.setUserId(imMsgBizUserHistory.getUserId());
            imMsgBizEmployeeRecent.setEmployeeId(imMsgBizUserHistory.getEmployeeId());
            imMsgBizEmployeeRecent.setEmployeeUnreadNum(0);
            imMsgBizEmployeeRecent.setCreateTime(new Date());
            imMsgBizEmployeeRecent.setUpdateTime(new Date());
            this.save(imMsgBizEmployeeRecent);
        } else {
            if (Objects.equals(imMsgBizUserHistory.getWhoSend(), WhoSend.USER)) {
                imMsgBizEmployeeRecent.setEmployeeUnreadNum(imMsgBizEmployeeRecent.getEmployeeUnreadNum() + 1);
            }
            imMsgBizEmployeeRecent.setUpdateTime(new Date());
            this.update(imMsgBizEmployeeRecent);
        }
    }
}
